package com.ycsj.goldmedalnewconcept.activity;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.constant.ConfigConstant;
import com.ycsj.goldmedalnewconcept.utils.MyWebViewClient;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;

/**
 * 错题集
 * @author 
 */
@SuppressLint("SetJavaScriptEnabled")
public class CuotijiActivity extends BaseJpStarActivity {
	private WebView webView;
	private WebView webView2;
	private FrameLayout video_fullView;// 全屏时视频加载view
	private View xCustomView;
	private ProgressDialog waitdialog = null;
	private CustomViewCallback xCustomViewCallback;
	private ProgressBar mprogressBar;
	private TextView tv_html_title;
	private TextView tv_word_back;
	private Intent intent;
	private String classnum;
	private String role;

	@SuppressLint("JavascriptInterface")
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_PROGRESS);
		setContentView(R.layout.activity_cuotiji);

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			setTranslucentStatus(true);
		}

		initData();


		SystemBarTintManager tintManager = new SystemBarTintManager(this);
		tintManager.setStatusBarTintEnabled(true);
		tintManager.setStatusBarTintResource(R.color.base_color);

		mprogressBar = (ProgressBar) findViewById(R.id.mProgress);

		tv_html_title = (TextView) findViewById(R.id.tv_html_title);
		tv_word_back = (TextView) findViewById(R.id.tv_word_back);
		tv_word_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		webView = (WebView) findViewById(R.id.webView);
		webView2 = (WebView) findViewById(R.id.webView2);
		lESSON_ID = getIntent().getStringExtra("LESSON_ID");
		String LESSON_NAME = getIntent().getStringExtra("LESSON_NAME");
		
		WebSettings settings = webView.getSettings();
		WebSettings settings2 = webView2.getSettings();

		settings.setJavaScriptEnabled(true);
		webView2.getSettings().setJavaScriptEnabled(true);
		webView2.getSettings().setBuiltInZoomControls(false);
		webView2.getSettings().setUseWideViewPort(true);
		webView2.getSettings().setRenderPriority(RenderPriority.HIGH);
		webView2.getSettings().setDatabaseEnabled(true);
		webView2.getSettings().setDomStorageEnabled(true);
		webView2.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
		webView2.getSettings().setPluginState(PluginState.ON);
		webView2.loadData("", "text/html", null);

		webView.getSettings().setAllowFileAccess(true);
		webView2.setWebViewClient(new MyWebViewClient(mprogressBar));
		webView2.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				// TODO Auto-generated method stub
				mprogressBar.setProgress(newProgress);
			}
		});

		int VERSION_SDK_ELEVEN = 14;
		webView.setVisibility(View.GONE);

		webView2.addJavascriptInterface(new WebAppInterface(this), "Android");

//		webView2.loadUrl(Constant.SERVER_tk + ":36356/jpjNewPage/question.html");
		webView2.loadUrl("https://moregolden.com:25697/errorListPage.html");

	}

	private void initData() {

		intent = getIntent();

		//获取班级ID
		classnum = intent.getStringExtra("num");

		//获取角色
		role = SPUtil.getString(CuotijiActivity.this, "role", "");

	}


	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {

			webView2.loadUrl("javascript:oldBack()");
			return true;
		}
		return super.onKeyDown(keyCode,event);
	}



	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		webView2.setVisibility(View.GONE);
		webView.removeAllViews();
		webView.destroy();
		webView2.removeAllViews();
		webView2.destroy();
		if (webView != null) {
			webView = null;
		}
		if (webView2 != null) {
			webView2 = null;
		}
		System.gc();
	}


	private WebChromeClient chromeClient = new WebChromeClient() {

		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			if (newProgress == 100) {
				
			}
		}
	};

	private WebViewClient client = new WebViewClient() {
		// 当页面加载完成之后会调用此方法
		@Override
		public void onPageFinished(WebView view, String url) {
			
		}
	};

	public int getPhoneSDKInt() {
		int version = 0;
		try {
			version = Integer.valueOf(Build.VERSION.SDK);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return version;
	}

	private String rid;
	
	private String lESSON_ID;

	private MediaPlayer mediaPlayer = new MediaPlayer();

	public class WebAppInterface {
		private Context mContext;
		private String param;
		/** Instantiate the interface and set the context */
		WebAppInterface(Context c) {
			mContext = c;
		}

		// 传递参数
		@JavascriptInterface
		public String getParam() {


			if("2".equals(role)){
				param = SPUtil.getString(mContext, ConfigConstant.USERNAME,
						"")
						+ ","
						+ role+","+classnum;
			}else{
				param = SPUtil.getString(mContext, ConfigConstant.USERNAME,
						"")
						+ ","
						+ role;
			}


			Log.e("test", "param=" + param);
			return param;
		}

		// 关闭界面
		@JavascriptInterface
		public void finish() {
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					CuotijiActivity.this.finish();
				}
			});
			Log.e("test", "finish");
			return;
		}
	}

	@TargetApi(19)
	private void setTranslucentStatus(boolean on) {
		Window win = getWindow();
		WindowManager.LayoutParams winParams = win.getAttributes();
		final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
		if (on) {
			winParams.flags |= bits;
		} else {
			winParams.flags &= ~bits;
		}
		win.setAttributes(winParams);
	}

	@Override
	public void finish() {
		ViewGroup view = (ViewGroup) getWindow().getDecorView();
		view.removeAllViews();
		super.finish();
	}
}








